/****************************************************
  @title: 数据结构实验
  @name: <实验2-1> 线性表(顺序表)的应用--我的图书馆
  @object:
      [实验目的]
          应用线性表解决问题.
          有若干图书,借出的书需要登记下来. 
          用两个线性表分别保存现有图书和借书信息,
          并实现增加新书,借书,还书,打印等功能.
          学习命令式程序界面的编写方法.
      [实验提示]
          1. 在book.h中描述了描述书的Book类型,还有
          几个有关函数函数可以调用. 
          2. 在sqlist.h中完成顺序表的各种操作. 
          3. 借书时,输入书名,若有存书,则从存书表中
          删除,插入借书表.
          4. 还书时,输入书名,若借过此书,则从借书表
          中删除,插入存书表. 
  @include:
      用到的头文件
  @usage:
      请查看"TO-DO列表",根据要求完成代码
  @copyright: BTC 2005, Zhuang Bo
  @author: Zhuang Bo
  @date: 2005
  @description:
*****************************************************/

#include <cstdlib>
#include <iostream>
using namespace std;

#include "book.h"
#define ElemType Book    //使用Book作为线性表元素的类型 
#include "sqlist.h"

SqList s;        //存书表 
SqList b;        //借书表 

void SystemInitialize();
void SystemTerminate();
void SystemRun();

int main(int argc, char *argv[])
{
    
    SystemInitialize();    
    SystemRun();    
    SystemTerminate();    
    
    system("PAUSE");
    return 0;
}

void SystemInitialize ()
{
    InitList ( s );
    InitList ( b );
    
    Book mybooks[] = {
        {"C Programming Language"},
        {"Data Structures"},
        {"C++ Programming Language"},
        {"Effactive C++"},
        {"More Effactive C++"},
        {"Design Patterns"}
    };    
    for ( int i=0; i<6; i++ )
        ListInsert( s, i, mybooks[i]); 
}

void SystemRun()
{
    char GetCommand();
    void DoCommand (char cmd);
    char cmd;
    
    do {
        cmd = GetCommand();
        DoCommand(cmd);
    } while ( cmd!='q' );
}

char GetCommand ()
{
    char str[64];
    cout << "\na:新书 b:借书 r:还书 p:打印 q:退出" << endl;
    cout << ".";
    cin.getline(str,64);
    return str[0];
}

void DoCommand ( char cmd )
{
    void AddNewBook();
    void BorrowBook();
    void ReturnBook();
    void PrintBookLists();
    
    switch ( cmd ) {
    case 'a':
        AddNewBook();
        break;
    case 'b':
        BorrowBook();
        break;
    case 'r':
        ReturnBook();
        break;
    case 'p':
        PrintBookLists();
        break;
    case 'q':
        break;
    default:
        cout << "\n命令错误" << endl; 
    }
}

void AddNewBook()
{
    Book abook;
    
    cout << "\n输入新书名:";
    ReadBook(abook);
    if ( ListInsert(s,1,abook) )
        cout << "入库成功" << endl;
    else
        cout << "入库失败" << endl;
}

void BorrowBook()
{
    Book abook;
    int k;
    
    cout << "\n借书书名:";
    ReadBook(abook);
    k = LocateElem(s,abook,BookEqual);
    if ( k!=0 ) {
        // TODO (#1#): 借书 
//        ListDelete(s,k,abook);
//        ListInsert(b,1,abook);
        //-------------------------------------

        cout << "\n借书成功" << endl;
    } else
        cout << "\n查无此书" << endl;
}

void ReturnBook()
{
    Book abook;
    int k;
    
    cout << "\n还书书名:";
    ReadBook(abook);
    k = LocateElem(b,abook,BookEqual);
    if ( k!=0 ) {
        // TODO (#1#): 还书 
//        ListDelete(b,k,abook);
//        ListInsert(s,1,abook);
        //-------------------------------------
        cout << "\n还书成功" << endl;
    } else
        cout << "\n查无此书" << endl;
}

void PrintBookLists()
{
    cout << "\n库存 " << ListLength(s) << " 本书:\n";
    ListTraverse ( s, PrintBook );
    cout << "\n借出 " << ListLength(b) << " 本书:\n";
    ListTraverse ( b, PrintBook );
}

void SystemTerminate () 
{
    DestroyList ( s );
    DestroyList ( b );
}
